import { createRouter, createWebHashHistory } from "vue-router";
import { LoadingBar } from "@varlet/ui";
import { loginStore } from "@/store/user";
//import { defineAsyncComponent } from 'vue';
const Layout = () => import("@/Layout/index.vue");
const Home = () => import("@/views/Home/index.vue");
const Clew = () => import("@/views/Clew/index.vue");
const Result = () => import("@/views/Result/index.vue");
const Login = () => import("@/views/Login/index.vue");
const toHistory = () => import("@/views/Historys/index.vue");
const LogDate = () => import("@/views/LogDate/index.vue");
const Users = () => import("@/views/User/index.vue");
const Register = () => import("@/views/Register/index.vue");
const HisResult = () => import("@/views/HisResult/index.vue");
const Protocol = () => import("@/views/Protocol/index.vue");
const Privacy = () => import("@/views/Privacy/index.vue");
const routes = [
  {
    path: "/",
    name: "Layout",
    meta: {
      requiresAuth: true,
    },
    component: Layout,
    redirect: "/home",
    children: [
      { path: "/home", name: "home", component: Home },
      { path: "/result", name: "result", component: Result },
      { path: "/clew", name: "clew", component: Clew },
      { path: "/historys", name: "historys", component: toHistory },
      { path: "/Users", name: "users", component: Users },
      { path: "/LogDate", name: "LogDate", component: LogDate },
      { path: "/HisResult", name: "HisResult", component: HisResult },
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登录",
      keepAlive: true,
      requireAuth: true,
    },
    component: Login,
  },
  {
    path: "/Register",
    name: "Register",
    meta: {
      title: "注册",
      keepAlive: true,
      requireAuth: true,
    },
    component: Register,
  },
  {
    path: "/Protocol",
    name: "Protocol",
    meta: {
      title: "注册",
      keepAlive: true,
      requireAuth: true,
    },
    component: Protocol,
  },
  {
    path: "/Privacy",
    name: "Privacy",
    meta: {
      title: "注册",
      keepAlive: true,
      requireAuth: true,
    },
    component: Privacy,
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  LoadingBar.start();
  //const store = loginStore()
  if (to.meta.requiresAuth) {
    let userInfo = JSON.parse(localStorage.getItem("login"));
    if (userInfo == null) {
      next({ name: "Login" });
    } else {
      next();
    }
  } else {
    next();
  }
});
router.afterEach(() => {
  LoadingBar.finish();
});
export default router;
